<?php
// 简单测试团队预约API
require_once '../config/database.php';

header('Content-Type: application/json; charset=utf-8');

echo "=== 团队预约API测试 ===\n";

try {
    // 1. 测试数据库连接
    $pdo = new PDO(
        'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET,
        DB_USER,
        DB_PASS,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
    
    echo "✓ 数据库连接成功\n";
    echo "数据库: " . DB_NAME . "\n";
    echo "用户: " . DB_USER . "\n";
    
    // 2. 检查表是否存在
    $stmt = $pdo->query("SHOW TABLES LIKE 'team_reservations'");
    $tableExists = $stmt->fetch();
    
    if ($tableExists) {
        echo "✓ team_reservations 表存在\n";
        
        // 3. 获取表结构
        $stmt = $pdo->query("DESCRIBE team_reservations");
        $columns = $stmt->fetchAll();
        echo "表结构:\n";
        foreach ($columns as $col) {
            echo "  - {$col['Field']} ({$col['Type']})\n";
        }
        
        // 4. 统计数据
        $stmt = $pdo->query("SELECT COUNT(*) as count FROM team_reservations");
        $count = $stmt->fetch()['count'];
        echo "数据记录数: {$count}\n";
        
        if ($count > 0) {
            // 5. 获取前3条数据
            $stmt = $pdo->query("SELECT * FROM team_reservations ORDER BY created_at DESC LIMIT 3");
            $data = $stmt->fetchAll();
            echo "\n前3条记录:\n";
            foreach ($data as $i => $record) {
                echo "记录" . ($i + 1) . ":\n";
                echo "  ID: {$record['id']}\n";
                echo "  团队名称: {$record['team_name']}\n";
                echo "  联系人: {$record['contact_name']}\n";
                echo "  状态: {$record['status']}\n";
                echo "  创建时间: {$record['created_at']}\n\n";
            }
        }
        
        // 6. 模拟API调用
        echo "=== 模拟API调用 ===\n";
        $sql = "SELECT 
            tr.*,
            au.username as approved_by_name
        FROM team_reservations tr 
        LEFT JOIN admin_users au ON tr.approved_by = au.id 
        WHERE 1=1 
        ORDER BY tr.created_at DESC 
        LIMIT 50 OFFSET 0";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $reservations = $stmt->fetchAll();
        
        $apiResponse = [
            'code' => 200,
            'message' => '获取成功',
            'data' => [
                'list' => $reservations,
                'pagination' => [
                    'page' => 1,
                    'limit' => 50,
                    'total' => $count,
                    'pages' => ceil($count / 50)
                ]
            ]
        ];
        
        echo "API响应数据:\n";
        echo json_encode($apiResponse, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
        
    } else {
        echo "✗ team_reservations 表不存在\n";
        
        // 显示所有表
        $stmt = $pdo->query("SHOW TABLES");
        $tables = $stmt->fetchAll();
        echo "数据库中的所有表:\n";
        foreach ($tables as $table) {
            $tableName = array_values($table)[0];
            echo "  - {$tableName}\n";
        }
    }
    
} catch (PDOException $e) {
    echo "✗ 数据库错误: " . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "✗ 其他错误: " . $e->getMessage() . "\n";
}
?>